from django.db.models import Q
from django.shortcuts import render
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.serializers import ModelSerializer
from rest_framework.views import APIView

from coures.courseser import CourseSer, CommSer, CourseIndexSerializer
from coures.models import CourseModel, CommModel
from home.NavSer import CateSer, Cate
from home.models import cateModel
from order.models import goumai


# 查询一级标题
# Create your views here.
class CateGoryAPI(APIView):
    def get(self,request):
        new=cateModel.objects.filter(is_del=0,parent=None).order_by("order")
        ser=Cate(new,many=True)
        return Response({"mas":"ok","code":200,"data":ser.data})
# 查询二级标题
class CateGory(APIView):
    def get(self,request):
        id=int(request.query_params.get("id"))
        if id!=0:
            data=cateModel.objects.filter(is_del=0,parent=id).order_by('order')
        else:
            data=cateModel.objects.filter( Q(is_del=0),~Q(parent = None)).order_by('order')
        ser=Cate(data,many=True)
        return Response({"mas":"ok","code":200,"data":ser.data})

# http://127.0.0.1:8000/uploads/img/1.jpeg

class CoursePage(PageNumberPagination):
    page_size = 2
    page_size_query_param = "size"
    page_query_param = "page"
#分页条件查询所有课程商品
class CourseAPI(ListAPIView):
    queryset = CourseModel.objects.all()
    serializer_class = CourseSer
    pagination_class = CoursePage

    def get_queryset(self):
        query=Q()
        cateid=int(self.request.query_params.get("cateid"))
        directionid=int(self.request.query_params.get("direction"))
        level=self.request.query_params.get("level","")
        ordering=self.request.query_params.get("ordering","")

        if cateid!=0:
            query&=Q(category=cateid)

        if directionid!=0 and cateid==0:
            data=cateModel.objects.filter(is_del=0,parent=directionid)
            cateList=[]
            for i in data:
                cateList.append(i.id)
            query&=Q(category__in=cateList)
        if level:
            query&=Q(level=level)
        if ordering:
            return self.queryset.filter(query).order_by(ordering)
        else:
            return self.queryset.filter(query)

# 根据ID查询则这个单个数据
class CouresID(APIView):
    def get(self,request):
        new=CourseModel.objects.filter(is_del=0,id=request.query_params.get("id")).first()
        ser=CourseSer(new)
        return Response({"mas":"ok","code":200,"data":ser.data})



# 用户评价展示
class CommitView(APIView):
    def get(self,request):
        courseid=request.query_params.get("courseid")

        cc=CommModel.objects.filter(CommentsModel=courseid)
        ser=CommSer(cc,many=True)
        return Response({"msg":"ok","code":200,"data":ser.data})

# 添加用户评价
class CommitAdd(APIView):
    def post(self,request):
        userid=request.query_params.get("userid")
        courseid=request.query_params.get("courseid")
        lis=goumai.objects.filter(userid=userid,Courseid=courseid)
        print(lis)
        if lis.__len__()==0:
            return Response({"msg":"仅购买后可以评论"})
        else:
            commit=request.query_params.get("commit")
            comp={
                'userCommentsModel':userid,
                'CommentsModel':courseid,
                'comments':commit,
            }
            ser=CommSer(data=comp)
            if ser.is_valid():
                ser.save()
                return Response({"msg":"评论成功"})

from drf_haystack.viewsets import HaystackViewSet
class HotWord(HaystackViewSet):
    index_models = [CourseModel]
    serializer_class = CourseIndexSerializer